package com.hospital.dao.impl;

import com.hospital.dao.DoctorViewDao;
import com.hospital.model.DoctorView;
import com.hospital.util.DruidUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 医生视图数据访问对象实现类
 * 实现对医生视图信息的数据库操作，包括查询、验证登录等功能
 */
public class DoctorViewDaoImpl implements DoctorViewDao {
    
    /**
     * 根据医生ID查找医生视图信息
     * @param doctorId 医生ID
     * @return 医生视图对象，如果未找到则返回null
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public DoctorView findByDoctorId(String doctorId) throws SQLException {
        String sql = "SELECT * FROM DoctorView WHERE doctorId = ?";
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, doctorId);
            ResultSet rs = pstmt.executeQuery();
            
            if (rs.next()) {
                return extractDoctorViewFromResultSet(rs);
            }
            return null;
        }
    }
    
    /**
     * 查询所有医生视图信息
     * @return 医生视图对象列表
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public List<DoctorView> findAll() throws SQLException {
        String sql = "SELECT * FROM DoctorView";
        List<DoctorView> doctors = new ArrayList<>();
        
        try (Connection conn = DruidUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                doctors.add(extractDoctorViewFromResultSet(rs));
            }
        }
        return doctors;
    }
    
    /**
     * 根据科室ID查询医生视图信息
     * @param deptId 科室ID
     * @return 医生视图对象列表
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public List<DoctorView> findByDeptId(int deptId) throws SQLException {
        String sql = "SELECT * FROM DoctorView WHERE deptId = ?";
        List<DoctorView> doctors = new ArrayList<>();
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, deptId);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                doctors.add(extractDoctorViewFromResultSet(rs));
            }
        }
        return doctors;
    }
    
    /**
     * 根据医生姓名模糊查询医生视图信息
     * @param doctorName 医生姓名
     * @return 医生视图对象列表
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public List<DoctorView> findByDoctorName(String doctorName) throws SQLException {
        String sql = "SELECT * FROM DoctorView WHERE doctorName LIKE ?";
        List<DoctorView> doctors = new ArrayList<>();
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, "%" + doctorName + "%");
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                doctors.add(extractDoctorViewFromResultSet(rs));
            }
        }
        return doctors;
    }
    
    /**
     * 验证医生登录信息
     * @param doctorId 医生ID
     * @param password 密码
     * @return 如果验证成功返回医生视图对象，否则返回null
     * @throws SQLException 如果数据库访问出错
     */
    @Override
    public DoctorView validateLogin(String doctorId, String password) throws SQLException {
        String sql = "SELECT * FROM doctorview WHERE doctorId = ? AND doctorPassword = ?";
        
        try (Connection conn = DruidUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setString(1, doctorId);
            pstmt.setString(2, password);
            ResultSet rs = pstmt.executeQuery();
            
            if (rs.next()) {
                return extractDoctorViewFromResultSet(rs);
            }
            return null;
        }
    }
    
    /**
     * 从ResultSet中提取医生视图信息
     * @param rs 包含医生视图信息的ResultSet对象
     * @return 提取的DoctorView对象
     * @throws SQLException 如果数据库访问出错
     */
    private DoctorView extractDoctorViewFromResultSet(ResultSet rs) throws SQLException {
        DoctorView doctor = new DoctorView();
        doctor.setDoctorId(rs.getString("doctorId"));
        doctor.setDoctorPassword(rs.getString("doctorPassword"));
        doctor.setDoctorName(rs.getString("doctorName"));
        doctor.setDoctorGender(rs.getString("doctorGender").charAt(0));
        doctor.setDoctorAge(rs.getInt("doctorAge"));
        doctor.setDoctorPhone(rs.getString("doctorPhone"));
        doctor.setDoctorIdCard(rs.getString("doctorIdCard"));
        doctor.setDeptId(rs.getInt("deptId"));
        doctor.setDeptName(rs.getString("deptName"));
        return doctor;
    }
} 